package com.qtyx.nms.custom

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.PopupWindow
import com.qtyx.nms.R

/**
 * 加载框
 */
class LoadingPopup(private val mActivity: Activity) : PopupWindow() {


    init {
        val dialogView =
            LayoutInflater.from(mActivity).inflate(R.layout.l_loading_popup, null)
        contentView = dialogView
        width = WindowManager.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        isFocusable = true

        //物理返回键有响应
        setBackgroundDrawable(BitmapDrawable())

        //窗口关闭事件
//        setOnDismissListener {
//            backgroundAlpha(1f);
//        }
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    fun backgroundAlpha(bgAlpha: Float) {
        val lp = mActivity.window.attributes
        lp.alpha = bgAlpha //0.0-1.0
        mActivity.window.attributes = lp
    }

    fun show() {
        //设置显示的位置
        showAtLocation(mActivity.window.decorView, Gravity.CENTER, 0, 0)
        //显示popupWindow时屏幕为半透明
//		backgroundAlpha(1f);
    }


}